package com.fcy.teamwork.bean

import cn.jpush.im.android.api.enums.ConversationType
import cn.jpush.im.android.api.model.Conversation
import cn.jpush.im.android.api.model.GroupInfo
import cn.jpush.im.android.api.model.UserInfo
import java.io.File

data class Contact(
    // userInfo userName groupInfo groupId
    var identify: String,
    var nickName: String,
    var hasUnRead: Boolean = false,
    var portrait: String = "",
    var type: ConversationType = ConversationType.single,
    var portraitFile: File? = null,
) {
    fun getBrand() = when (type) {
        ConversationType.chatroom -> {
            "chatroom"
        }
        ConversationType.group -> {
            "group"
        }
        ConversationType.single -> {
            "single"
        }
        else -> {
            throw IllegalArgumentException("位置参数错误")
        }
    }
}

fun Conversation.toContact(): Contact =
    when (type) {
        ConversationType.single -> {
            val userInfo = targetInfo as UserInfo
            Contact(
                userInfo.userName,
                userInfo.nickname,
                portraitFile = userInfo.avatarFile,
                hasUnRead = unReadMsgCnt != 0
            )
        }
        ConversationType.group -> {
            val group = targetInfo as GroupInfo
            Contact(
                group.groupID.toString(),
                group.groupName,
                portraitFile = group.avatarFile,
                hasUnRead = unReadMsgCnt != 0,
                type = ConversationType.group
            )
        }
        ConversationType.chatroom -> {
            Contact("", "")
        }
        else -> {
            throw IllegalStateException("未知的ConversationType")
        }
    }
